1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.concurrent.ConcurrentLinkedQueue;
25 import java.util.concurrent.atomic.AtomicBoolean;
26 import org.apache.maven.surefire.report.ReportEntry;
27 import org.apache.maven.surefire.report.RunListener;
28 import org.apache.maven.surefire.report.SimpleReportEntry;
29
30 import org.junit.runner.Description;
31
32
33
34
35 public class TestSet
36 {
37 private static final InheritableThreadLocal<TestSet> TEST_SET = new InheritableThreadLocal<TestSet>();
38
39 private final Description testSetDescription;
40
41 private final Collection<TestMethod> testMethods = new ConcurrentLinkedQueue<TestMethod>();
42
43 private final AtomicBoolean played = new AtomicBoolean();
44
45 private volatile boolean allScheduled;
46
47 private volatile int numberOfCompletedChildren;
48
49
50
51
52 private volatile int numberOfTests;
53
54 public TestSet( Description testSetDescription )
55 {
56 this.testSetDescription = testSetDescription;
57 }
58
59 public void replay( RunListener target )
60 {
61 if ( played.compareAndSet( false, true ) )
62 {
63 try
64 {
65 ReportEntry report = createReportEntry( null );
66
67 target.testSetStarting( report );
68
69 long startTile = 0;
70 long endTime = 0;
71 for ( TestMethod testMethod : testMethods )
72 {
73 if ( startTile == 0 || testMethod.getStartTime() < startTile )
74 {
75 startTile = testMethod.getStartTime();
76 }
77
78 if ( endTime == 0 || testMethod.getEndTime() > endTime )
79 {
80 endTime = testMethod.getEndTime();
81 }
82
83 testMethod.replay( target );
84 }
85
86 int elapsed = (int) ( endTime - startTile );
87
88 report = createReportEntry( elapsed );
89
90 target.testSetCompleted( report );
91 }
92 catch ( Exception e )
93 {
94 throw new RuntimeException( e );
95 }
96 }
97 }
98
99 public TestMethod createThreadAttachedTestMethod( ReportEntry description )
100 {
101 TestMethod testMethod = new TestMethod( description, this );
102 addTestMethod( testMethod );
103 testMethod.attachToThread();
104 return testMethod;
105 }
106
107 private ReportEntry createReportEntry( Integer elapsed )
108 {
109 final String className = testSetDescription.getClassName();
110 final boolean isJunit3 = className == null;
111 final String classNameToUse;
112 if ( isJunit3 )
113 {
114 List<Description> children = testSetDescription.getChildren();
115 classNameToUse = children.isEmpty() ? testSetDescription.toString() : children.get( 0 ).getClassName();
116 }
117 else
118 {
119 classNameToUse = className;
120 }
121 return new SimpleReportEntry( classNameToUse, classNameToUse, elapsed );
122 }
123
124 public void incrementTestMethodCount()
125 {
126 numberOfTests++;
127 }
128
129 private void addTestMethod( TestMethod testMethod )
130 {
131 testMethods.add( testMethod );
132 }
133
134 public void incrementFinishedTests( RunListener reporterManager, boolean reportImmediately )
135 {
136 numberOfCompletedChildren++;
137 if ( allScheduled && isAllTestsDone() && reportImmediately )
138 {
139 replay( reporterManager );
140 }
141 }
142
143 public void setAllScheduled( RunListener reporterManager )
144 {
145 allScheduled = true;
146 if ( isAllTestsDone() )
147 {
148 replay( reporterManager );
149 }
150 }
151
152 private boolean isAllTestsDone()
153 {
154 return numberOfTests == numberOfCompletedChildren;
155 }
156
157 public void attachToThread()
158 {
159 TEST_SET.set( this );
160 }
161
162 public static TestSet getThreadTestSet()
163 {
164 return TEST_SET.get();
165 }
166 }